Skip to content

Add AMP-first preview and args to validate request for Site Scanning #6615

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 20 commits into from
Oct 26, 2021

Conversation

westonruter
Copy link
Member

@westonruter westonruter commented Sep 18, 2021

Summary

Validate Request Args

The amp_validate query var has been improved so that instead of taking a single string value for the nonce, it instead can be an array of args, including:

  • nonce (string): What will be checked against AMP_Validation_Manager::get_amp_validate_nonce()
  • cache (bool): Whether the validation results will be stored in an amp_validated_url post.
  • cached_if_fresh (bool): Whether previously-stored results will be returned if there is a non-stale amp_validated_url post for the current URL.
  • omit_stylesheets (bool): Whether to omit the stylesheets key from the response.

For example, attempting to get the getting results for the homepage can look like a request to the following URL:

https://wordpressdev.lndo.site/?amp_validate[nonce]=e9ca7665d29fab856e78a6ff23da8979&amp_validate[omit_stylesheets]=true&amp_validate[cached_if_fresh]=true&amp_validate[cache]=true

When such a cached request is made, the response will include a new key:

"validated_url_post": {
    "id": 1,
    "edit_link": "https://wordpressdev.lndo.site/core-dev/src/wp-admin/post.php?post=3778&action=edit"
}

The edit link here is the Validated URL screen where the results can be viewed. See #6610 (comment). Partially addresses #4979.

A revalidated prop is also included which indicates whether the previously-cached data was returned (if false) or whether the page was re-validated (true).

AMP-first Override

The AMP plugin's default template mode is Reader Nevertheless, when doing Site Scanning we want to check the site as if Standard template mode is active, so we can check the active theme for AMP compatibility issues. In order to facilitate this, a new amp-first query parameter is introduced which allows an authenticated user or a validation request force Standard mode. The logic employed for this override is adopted back from the Web Stories plugin first introduced in GoogleForCreators/web-stories-wp#3621. The conditions are a bit convoluted, but much of the admin-specific logic will be able to be eliminated once we move away from classic admin screens in favor of doing more RESTful requests for validation data. See #4719 (comment).

Checklist

  • My code is tested and passes existing tests.
  • My code follows the Engineering Guidelines (updates are often made to the guidelines, check it out periodically).

@westonruter westonruter added this to the v2.2 milestone Sep 18, 2021
@westonruter westonruter mentioned this pull request Sep 18, 2021
6 tasks
…ate-store-url-query-var

* 'develop' of github.com:ampproject/amp-wp: (67 commits)
  Ignore `nested-interactive` Axe rule
  Set appropriate node and npm versions in package.json
  Downgrade package-lock.json back to v1
  Improve test coverage
  Add special case logic for gutenberg_inject_default_block_context
  Improve method ordering
  Remove needless check for filter-specific add_block_source_comments
  Run wrap_block_callbacks at PHP_INT_MIN
  Update handle_block_source_comment_replacement to handle wrappers added in wrap_block_callbacks
  Simplify logic in wrap_block_callbacks
  Bump svgo from 2.6.0 to 2.6.1
  Advise to check loopback request test
  Simplify language for condition for what headers are looked for
  Bump eslint-plugin-jest from 24.4.0 to 24.4.2
  Improve test coverage for get_persistent_object_cache_learn_more_action
  Remove absolete wp_doing_ajax filter
  Eliminate use of deprecated assertion
  Store results of page caching to reduce severity of missing a persistent object cache
  Explain how page cache detection is performed
  Improve tests for page caching
  ...
@westonruter westonruter changed the title Allow validate requests to also store results Add AMP-first preview for Site Scanning and allow validate requests to also store results Sep 27, 2021
@westonruter westonruter marked this pull request as ready for review September 27, 2021 19:20
@westonruter westonruter requested a review from pierlon September 27, 2021 19:20
@github-actions
Copy link
Contributor

github-actions bot commented Sep 27, 2021

Plugin builds for c226596 are ready 🛎️!

* @see \AMP_Validation_Manager::has_cap()
* @var string
*/
const AMP_FIRST = 'amp-first';
Copy link
Member Author

@westonruter westonruter Sep 28, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that the hyphenated amp-first is used because it's intended to be user-facing.

The new amp_store query parameter is not user-facing, and goes along with amp_validate. So that's why I chose an underscore.

@westonruter
Copy link
Member Author

westonruter commented Sep 30, 2021

  • Also add a parameter to allow returning cached results if they exist.
  • Indicate if the results are stale (via \AMP_Validated_URL_Post_Type::get_post_staleness()) when passing amp_store. Include as meta information under amp_validated_url.
  • The \AmpProject\AmpWP\Validation\ScannableURLProvider::get_urls() method should return results based on the currently-selected template mode. If using legacy reader theme, only the singular post or page should be returned as URLs. (Or rather, they can just skip since AMP is not available.)
  • The Settings screen (and WP Cron) should run a scan based on the selected template mode, whereas the Onboarding WIzard should force AMP-first.

@westonruter
Copy link
Member Author

westonruter commented Oct 6, 2021

@delawski One more thing I expect will be useful for you:

  • Allow omission of stylesheet data from response via a query parameter.

Since the stylesheet data is by far the most massive part of the validation response, excluding it should make the screens load faster.

@delawski
Copy link
Collaborator

delawski commented Oct 6, 2021

Allow omission of stylesheet data from response via a query parameter.

It sounds really good! Stylesheet data is huge and we don't really need it for Site Scan.

@delawski
Copy link
Collaborator

delawski commented Oct 6, 2021

The \AmpProject\AmpWP\Validation\ScannableURLProvider::get_urls() method should return results based on the currently-selected template mode. If using legacy reader theme, only the singular post or page should be returned as URLs. (Or rather, they can just skip since AMP is not available.)

One thing to note here is that we are fetching the scannable URLs at the beginning of the Onboarding Wizard flow. They are used later in the Site Scan. I guess we should be able to override the template mode when retrieving the URLs so that we always scan against the Standard mode.

@westonruter westonruter requested a review from delawski October 7, 2021 04:50
@westonruter
Copy link
Member Author

One thing to note here is that we are fetching the scannable URLs at the beginning of the Onboarding Wizard flow. They are used later in the Site Scan. I guess we should be able to override the template mode when retrieving the URLs so that we always scan against the Standard mode.

In regards to the URLs returned by ScannableURLProvider::get_urls() needing to include all templates even when legacy Reader mode is selected: this seems to be the case. The reason is that ScannableURLProvider is not being constructed with any URLScanningContext, so this is resulting in all templates being returned. So at the moment you should be in good shape to use the existing endpoint you have.

@westonruter westonruter changed the title Add AMP-first preview for Site Scanning and allow validate requests to also store results Add AMP-first preview and args to validate request for Site Scanning Oct 7, 2021
@westonruter
Copy link
Member Author

westonruter commented Oct 7, 2021

@delawski I suppose after this PR we could eliminate the URLValidationRESTController service entirely since it is redundant while also being more limited since it only allows for validation of posts and also slower in that it requires a loopback request. Naturally this change could be done in a subsequent PR, assuming the changes in this PR check out.

@delawski delawski force-pushed the add/validate-store-url-query-var branch from 9f3a80c to 5b46ffd Compare October 13, 2021 13:34
@delawski delawski force-pushed the add/validate-store-url-query-var branch from 5b46ffd to 5fc2692 Compare October 25, 2021 11:33
@westonruter westonruter force-pushed the add/validate-store-url-query-var branch from 5fc2692 to 9f3a80c Compare October 25, 2021 18:35
*
* @param array $sanitization_results Sanitization results.
* @param int $status_code Status code.
* @param array|null $last_error Last error.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a thought, Instead of taking $last_error as an argument, can we assign it in the function itself? Since, it is return value of error_get_last();

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could, but I thought this may be preferable since it is explicit input and less reliance on global state, so it is easier to test.

Copy link
Collaborator

@dhaval-parekh dhaval-parekh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes look good to me.

@westonruter westonruter merged commit 746d7ac into develop Oct 26, 2021
@westonruter westonruter deleted the add/validate-store-url-query-var branch October 26, 2021 00:02
@fellyph fellyph self-assigned this Dec 9, 2021
@fellyph
Copy link
Collaborator

fellyph commented Dec 10, 2021

QA Passed

  • cache
  • cache_bust
  • nonce
  • omit_stylesheets

Requests
Screen Shot 2021-12-10 at 11 36 33
Screen Shot 2021-12-10 at 11 36 06
Screen Shot 2021-12-10 at 11 35 54

@fellyph fellyph removed their assignment Dec 10, 2021
@westonruter westonruter added the Changelogged Whether the issue/PR has been added to release notes. label Dec 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Changelogged Whether the issue/PR has been added to release notes. Site Scanning
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants